package com.example.listview2;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ListWorkApdapter extends ArrayAdapter<Work> 
{
	ArrayList<Work> array;
	int resource;
	Context context;
	
	public ListWorkApdapter(Context context, int textViewResourceId, ArrayList<Work> objects)
	{
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		resource = textViewResourceId;
		array = objects;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View workView = convertView;
		if(workView == null)
		{
			workView = new CustomViewGroup(getContext());
		}
		
		final Work work = array.get(position);
		
		if(work != null)
		{
			TextView congViec =((CustomViewGroup)workView).congViec;
			TextView thoiGian = ((CustomViewGroup)workView).thoiGian;
			CheckBox cb = ((CustomViewGroup)workView).cb;
			
			cb.setOnCheckedChangeListener(new OnCheckedChangeListener() 
			{
				//@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
				{
					// TODO Auto-generated method stub
					work.setChecked(isChecked);
				}
			});
			congViec.setText(work.congViec());
			thoiGian.setText(work.thoiGian());
			cb.setChecked(work.isChecked());
		}
		return workView;
	}
}
